package com.dyypdadmin.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dyypdadmin.pojo.vo.AppointmentRecordsDetail;
import com.dyypdadmin.pojo.vo.CategoriesCount;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface AppointmentRecordsDetailDao extends BaseMapper<AppointmentRecordsDetail> {
    //多表连接查询所有的信息
    @Select("select appointment_pkid,user_pkid,username,pet_name,service_name,doctor_name,appointment_date,appointment_time,a.is_del,appointment_status,is_medical from appointmentrecords a join users u on u.user_id = a.user_pkid join pets p on a.pet_id = p.pet_id join servicedetails s on a.project_id = s.service_detail_id join doctors d on d.doctor_id = a.doctor_pkid")
    List<AppointmentRecordsDetail> getAll();

    //根据用户id进行查询
    @Select("select appointment_pkid,p.pet_id,user_pkid,username,pet_name,service_name,doctor_name,appointment_date,appointment_time,a.is_del,appointment_status,is_medical from appointmentrecords a join users u on u.user_id = a.user_pkid join pets p on a.pet_id = p.pet_id join servicedetails s on a.project_id = s.service_detail_id join doctors d on d.doctor_id = a.doctor_pkid where user_pkid=#{user_pkid}")
    List<AppointmentRecordsDetail> getAllByUserId(@Param("user_pkid") Integer user_pkid);

    //根据预约时间进行查询
    @Select("select appointment_pkid, p.pet_id, user_pkid, username, pet_name, service_name, doctor_name, appointment_date, appointment_time, a.is_del, appointment_status, is_medical \n" +
            "from appointmentrecords a \n" +
            "join users u on u.user_id = a.user_pkid \n" +
            "join pets p on a.pet_id = p.pet_id \n" +
            "join servicedetails s on a.project_id = s.service_detail_id \n" +
            "join doctors d on d.doctor_id = a.doctor_pkid \n" +
            "where DATE(appointment_time) =#{appointment_time}")
    List<AppointmentRecordsDetail> getAppointmentListByAppointmentTime(@Param("appointment_time") String appointment_time);

    //根据用户名进行查询
    @Select("select appointment_pkid,p.pet_id,user_pkid,username,pet_name,service_name,doctor_name,appointment_date,appointment_time,a.is_del,appointment_status,is_medical from appointmentrecords a join users u on u.user_id = a.user_pkid join pets p on a.pet_id = p.pet_id join servicedetails s on a.project_id = s.service_detail_id join doctors d on d.doctor_id = a.doctor_pkid where username like #{username}")
    List<AppointmentRecordsDetail> getAppointmentListByUsername(@Param("username") String username);

    //根据service_type_id查询出每个类别对应的条数
    @Select("select service_name as name ,count(service_detail_id) as value from appointmentrecords join servicedetails s on appointmentrecords.project_id = s.service_detail_id group by service_detail_id")
    List<CategoriesCount> getCountByServiceType();

    @Select("select appointment_pkid,p.pet_id,user_pkid,username,pet_name,service_name,doctor_name,appointment_date,appointment_time,a.is_del,appointment_status,is_medical from appointmentrecords a join users u on u.user_id = a.user_pkid join pets p on a.pet_id = p.pet_id join servicedetails s on a.project_id = s.service_detail_id join doctors d on d.doctor_id = a.doctor_pkid where user_pkid=#{user_pkid} and a.appointment_status='待服务'")
    List<AppointmentRecordsDetail> getWaitServeByUserId(@Param("user_pkid")Integer user_pkid);

    @Select("select appointment_pkid,p.pet_id,user_pkid,username,pet_name,service_name,doctor_name,appointment_date,appointment_time,a.is_del,appointment_status,is_medical from appointmentrecords a join users u on u.user_id = a.user_pkid join pets p on a.pet_id = p.pet_id join servicedetails s on a.project_id = s.service_detail_id join doctors d on d.doctor_id = a.doctor_pkid where user_pkid=#{user_pkid} and a.appointment_status='院方拒绝' or a.appointment_status='用户取消'")
    List<AppointmentRecordsDetail> getIncompleteByUserId(@Param("user_pkid")Integer user_pkid);

    @Select("select appointment_pkid,p.pet_id,user_pkid,username,pet_name,service_name,doctor_name,appointment_date,appointment_time,a.is_del,appointment_status,is_medical from appointmentrecords a join users u on u.user_id = a.user_pkid join pets p on a.pet_id = p.pet_id join servicedetails s on a.project_id = s.service_detail_id join doctors d on d.doctor_id = a.doctor_pkid where user_pkid=#{user_pkid} and a.appointment_status='服务完成'")
    List<AppointmentRecordsDetail> getCompletedByUserId(@Param("user_pkid")Integer user_pkid);

    @Select("SELECT COUNT(appointment_pkid) FROM appointmentrecords WHERE DATE(appointment_time) = CURDATE() AND appointment_status = '待服务'")
    int getTodayAppointmentsCount();

}
